//
//  HELStarRatingAlert.m
//  HandheldELearn
//
//  Created by wsj2015 on 16/7/12.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELStarRatingAlert.h"
#import "HCSStarRatingView.h"
@implementation HELStarRatingAlert
{
    UIButton *_btn;
    HCSStarRatingView *_ratingView;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        
    }
    return self;
}
- (void)layoutSubviews{

    [super layoutSubviews];
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        CGFloat myW = frame.size.width;
        CGFloat myH = frame.size.height;
        
        
        UILabel *label =[UILabel new];
        label.frame = CGRectMake(0, 0, myW, 30);
        [self addSubview:label];
        label.text = @"评分";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont boldSystemFontOfSize:17];
        label.backgroundColor = kMainColor;
        label.textColor = [UIColor whiteColor];
      
        CGFloat w = 160;
        HCSStarRatingView *ratingView =[[HCSStarRatingView alloc] initWithFrame:CGRectMake((myW - w)/2, CGRectGetMaxY(label.frame)+ 14, w, 25)];
        ratingView.maximumValue = 5;
        ratingView.minimumValue = 0;
        ratingView.allowsHalfStars = YES;
        ratingView.emptyStarImage =[UIImage imageNamed:@"icon_ratingStar_empty"];
        ratingView.filledStarImage =[UIImage imageNamed:@"icon_ratingStar_full"];
         ratingView.halfStarImage =[UIImage imageNamed:@"icon_ratingStar_half"];
        [self addSubview:ratingView];
        [ratingView addObserver:self forKeyPath:@"value" options:NSKeyValueObservingOptionNew context:nil];
        _ratingView  =ratingView;
        UIButton *btn =[UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitle:@"确认" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        btn.enabled = NO;
        btn.frame =CGRectMake(0, myH-30, myW, 30);
        [btn addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        btn.titleLabel.font = [UIFont systemFontOfSize:17];
        _btn = btn;
        
        UIView *line =[UIView new];
        line.frame = CGRectMake(0, CGRectGetMinY(btn.frame) -6, myW, 1);
        line.backgroundColor =kBorderColor;
        [self addSubview:line];
        
        
        
        label.tag = 200;
        ratingView.tag = 201;
        btn.tag =202;
        
        wsj_cornerRadius(self, 10);
        self.layer.borderWidth = 1;
        self.layer.borderColor = kMainColor.CGColor;
        
    }
    return self;
}
- (void)clickAction{
    
      HCSStarRatingView *r = (HCSStarRatingView *)[self viewWithTag:201];
   
    if (self.callBack) {
      
        self.callBack(r.value);
    }

    

}

- (void)dealloc{
    [_ratingView removeObserver:self forKeyPath:@"value"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"value"]) {
        NSNumber *value =  change[@"new"];
        if (value.floatValue <=0) {
            _btn.enabled = NO;
        }else{
            _btn.enabled = YES;
        }
    }

}
- (void)setRating:(CGFloat)rating{
       HCSStarRatingView *r = (HCSStarRatingView *)[self viewWithTag:201];
    r.value = rating;
    
    
}

- (void)setCustomTitle:(NSString *)customTitle{
    UILabel *label = [self viewWithTag:200];
    label.text = customTitle;
}

- (void)setIsAllowEdit:(BOOL)isAllowEdit{
     HCSStarRatingView *r = (HCSStarRatingView *)[self viewWithTag:201];
    r.userInteractionEnabled = isAllowEdit;
}
@end
